#!/bin/bash
echo "开始处理支付中心docker编译发布"
#初始化脚本存放目录
init_script_dir=/data/app/
#应用部署目录
app_dir=/data/app/
#应用日志部署目录
logs_dir=/logs/yunpeng/japan_pad_api
#应用镜像名称
docker_image_name=japan_pad_api
#应用镜像标签
docker_image_tag=0.0.1
#应用容器名称
docker_container_name=japan_pad_api
#应用容器是否重新编译过
rebuild=0
cd /

if [ ! -d "$logs_dir" ]; then
    echo "创建文件夹$logs_dir"
    mkdir -p $logs_dir
fi
if [ ! -d "/logs/nginx" ]; then
    echo "创建文件夹/logs/nginx"
    mkdir -p /logs/nginx
fi

cd $init_script_dir
# 应用容器处理
echo -e "请确认是否重新编译容器和镜像，y-更新（重新安装），其他-不更新（仅发布jar），回车确认 > \c"
read update_web
printf "\n"
if [ "$update_web" == "y" -o "$update_webupdate_web" == "Y" ]; then
    echo "选择了安装新的镜像容器"
    if [ ! -f "$init_script_dir/Dockerfile" ]; then
        echo "dockerfile不存在，1秒后退出"
        exit 1
    fi
    echo "检查容器是否已存在，存在则停止并删除此容器"
    if [ "$(docker ps -a | grep -E "$docker_container_name" | awk '{print $NF}')" == "$docker_container_name" ]; then
        echo "容器：$docker_container_name已启动"
        echo "已停止$(docker container stop $docker_container_name) "
        echo "已删除$(docker container rm $docker_container_name) "
    fi
    echo "开始删除已有镜像"
    docker images | grep -E "($docker_image_name)" | awk '{print $3}' | uniq | xargs -I {} docker rmi --force {}
    if [ $? -eq 0 ]; then
        echo "删除成功"
    else
        echo "删除失败，脚本退出，请手动检查是否存在其他容器依赖此镜像"
        exit 1
    fi
    echo "$PWD"
    echo "开始编译新镜像"
    docker build -t $docker_image_name:$docker_image_tag .
    if [ $? -eq 0 ]; then
        echo "镜像编译完成"
    else
        echo "镜像编译失败，脚本退出，请检查编译异常"
        exit 1
    fi
    docker run -itd --name $docker_container_name -p 8888:8080 \
     -v $logs_dir:$logs_dir \
     -v $app_dir:/web \
     -v /data/upload/files:/data/upload/files \
     -v /etc/localtime:/etc/localtime \
     -e TZ="Asia/Shanghai" \
     $docker_image_name:$docker_image_tag
    if [ $? -eq 0 ]; then
        echo "容器启动成功"
    else
        echo "容器启动失败，脚本退出，请检查启动异常"
        exit 1
    fi
    #已重新启动过
    rebuild=1
else
    echo "选择了不更新，即将使用已有镜像容器"
fi

if [ -f "$app_dir/japan_pad_api.jar" ]; then
    if [ $rebuild == 0 ]; then
        echo "开始重启容器$docker_container_name"
        docker container restart $docker_container_name
    fi
else
    echo "jar包不存在，请核查后重启容器： $docker_container_name"
fi
#nginx处理
echo -e "请确认是否更新nginx,y-更新（重新安装），其他-无需重启，回车确认 > \c"
read update_nginx
printf "\n"
if [ "$update_nginx" == "y" -o "$update_nginx" == "Y" ]; then
    if [ ! -f "$init_script_dir/nginx.conf" ]; then
        echo "nginx.conf不存在，1秒后退出"
        exit 1
    fi
    if [ "$(docker images | grep -E "nginx" | awk '{print $1}')" != "nginx" ]; then
    	echo "nginx镜像不存在，拉取最新nginx镜像"
        docker pull nginx:latest
    fi
    if [ "$(docker ps -a | grep -E "nginx" | awk '{print $NF}')" == "nginx" ]; then
        echo "已重启$(docker container restart nginx) "
    else
        docker run -itd --name nginx -p 80:80  -p 443:443\
         -v $init_script_dir/nginx.conf:/etc/nginx/nginx.conf \
         -v /logs/nginx:/var/log/nginx \
         -v /data/web/www:/usr/share/nginx/html \
         -v /data/web/Nginx:/usr/share/nginx/conf \
         -v /data/upload/files:/data/upload/files \
         nginx:latest
    fi
else
    echo "选择了无需重启nginx"
fi
echo "应用部署完成"